package com.lkdont.android.media.examples.ui

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.navigation.NavController
import androidx.navigation.Navigation
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.lkdont.android.base.ui.BaseFrag
import com.lkdont.android.media.examples.R
import com.lkdont.android.media.examples.entity.Example
import com.lkdont.android.media.examples.entity.ExampleGroup
import com.lkdont.android.media.examples.entity.Examples
import kotlinx.android.synthetic.main.frag_example_list.*

/*** 例子数据适配器 */
private class ExampleAdapter(
    exampleList: List<ExampleGroup>,
    private val onItemClickListener: (itemView: View, example: Example) -> Unit
) : RecyclerView.Adapter<RecyclerView.ViewHolder>() {

    companion object {
        private const val TYPE_GROUP = 1
        private const val TYPE_ITEM = 2
    }

    class ExampleGroupHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        val text1: TextView = itemView.findViewById(android.R.id.text1)
    }

    class ExampleHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        val text1: TextView = itemView.findViewById(android.R.id.text1)
        val text2: TextView = itemView.findViewById(android.R.id.text2)
    }

    private val exampleList: MutableList<Any> = ArrayList()

    init {
        exampleList.forEach { group ->
            this.exampleList.add(group)
            group.examples?.forEach {
                this.exampleList.add(it)
            }
        }
    }

    override fun getItemCount(): Int {
        return exampleList.size
    }

    override fun getItemViewType(position: Int): Int {
        return when (val item = exampleList[position]) {
            is ExampleGroup -> TYPE_GROUP
            is Example -> TYPE_ITEM
            else -> throw UnsupportedOperationException("不支持这个类型的item($item)")
        }
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
        return when (viewType) {
            TYPE_GROUP -> {
                ExampleGroupHolder(
                    LayoutInflater.from(parent.context)
                        .inflate(R.layout.item_example_group, parent, false)
                )
            }
            TYPE_ITEM -> {
                ExampleHolder(
                    LayoutInflater.from(parent.context)
                        .inflate(R.layout.item_example, parent, false)
                )
            }
            else -> throw UnsupportedOperationException("不支持这个类型的viewType($viewType)")
        }
    }

    override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
        val item = exampleList[position]
        when {
            (item is ExampleGroup) && (holder is ExampleGroupHolder) -> {
                holder.text1.setText(item.nameResId)
            }
            (item is Example) && (holder is ExampleHolder) -> {
                holder.text1.setText(item.nameResId)
                holder.text2.setText(item.descriptionResId)
                holder.itemView.setOnClickListener {
                    onItemClickListener(holder.itemView, item)
                }
            }
        }
    }
}

/**
 * 例子列表页面
 *
 * @author lkdont
 */
class ExampleListFrag : BaseFrag() {

    companion object {
        private val TAG = ExampleListFrag::class.java.simpleName
    }

    private val navController: NavController by lazy {
        Navigation.findNavController(
            requireActivity(),
            R.id.nav_host_fragment
        )
    }

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        return inflater.inflate(
            R.layout.frag_example_list,
            container,
            false
        )
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        val examples = ArrayList<ExampleGroup>()
        // 音频测试例子
        examples.add(
            ExampleGroup(
                R.string.example_group_audio,
                Examples.AUDIO_EXAMPLES
            )
        )
        // 视频测试例子
        examples.add(
            ExampleGroup(
                R.string.example_group_video,
                Examples.VIDEO_EXAMPLES
            )
        )
        // 图像测试例子
        examples.add(ExampleGroup(R.string.example_group_image, null))

        exampleRv.apply {
            layoutManager = LinearLayoutManager(requireContext())
            adapter = ExampleAdapter(examples) { _, example ->
                when (example) {

                    Examples.EXAMPLE_RECORD_AUDIO -> {
                        navController.navigate(ExampleListFragDirections.actionExampleListFragToRecordAudioFrag())
                    }

                    Examples.EXAMPLE_RECORD_CAMERA_VIDEO_1 -> {
                        navController.navigate(ExampleListFragDirections.actionExampleListFragToRecordCameraVideoFrag1())
                    }

                    Examples.EXAMPLE_RECORD_CAMERA_VIDEO_2 -> {
                        navController.navigate(ExampleListFragDirections.actionExampleListFragToRecordCameraVideoFrag2())
                    }

                    else -> {
                        showMessage(R.string.error_unsupported_example)
                    }
                }
            }
        }
    }
}
